package com.ruoyi.docdraw.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.docdraw.domain.supplier.SysSupplierVo;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.docdraw.mapper.SysSupplierMapper;
import com.ruoyi.docdraw.domain.SysSupplier;
import com.ruoyi.docdraw.service.ISysSupplierService;

/**
 * 组织供应商Service业务层处理
 * 
 * @author lvtian
 * @date 2025-04-02
 */
@Service
public class SysSupplierServiceImpl implements ISysSupplierService 
{
    @Autowired
    private SysSupplierMapper sysSupplierMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;
    /**
     * 查询组织供应商
     * 
     * @param supplierId 组织供应商主键
     * @return 组织供应商
     */
    @Override
    public SysSupplierVo selectSysSupplierBySupplierId(Long supplierId)
    {
        SysSupplier sysSupplier = sysSupplierMapper.selectSysSupplierBySupplierId(supplierId);
        Long deptId = sysSupplier.getSupplierId();
        SysDept sysDept = sysDeptMapper.selectDeptById(deptId);
        SysSupplierVo vo = new SysSupplierVo();

        vo.setSysDept(sysDeptMapper.selectDeptById(supplierId));
        BeanUtils.copyBeanProp(vo, sysSupplier);
        return vo;
    }

    /**
     * 查询组织供应商列表
     * 
     * @param sysSupplier 组织供应商
     * @return 组织供应商
     */
    @Override
    public List<SysSupplier> selectSysSupplierList(SysSupplier sysSupplier)
    {
        return sysSupplierMapper.selectSysSupplierList(sysSupplier);
    }

    /**
     * 新增组织供应商
     * 
     * @param sysSupplier 组织供应商
     * @return 结果
     */
    @Override
    public int insertSysSupplier(SysSupplier sysSupplier)
    {
        sysSupplier.setCreateTime(DateUtils.getNowDate());
        return sysSupplierMapper.insertSysSupplier(sysSupplier);
    }

    /**
     * 修改组织供应商
     * 
     * @param sysSupplier 组织供应商
     * @return 结果
     */
    @Override
    public int updateSysSupplier(SysSupplier sysSupplier)
    {
        sysSupplier.setUpdateTime(DateUtils.getNowDate());
        return sysSupplierMapper.updateSysSupplier(sysSupplier);
    }

    /**
     * 批量删除组织供应商
     * 
     * @param supplierIds 需要删除的组织供应商主键
     * @return 结果
     */
    @Override
    public int deleteSysSupplierBySupplierIds(Long[] supplierIds)
    {
        return sysSupplierMapper.deleteSysSupplierBySupplierIds(supplierIds);
    }

    /**
     * 删除组织供应商信息
     * 
     * @param supplierId 组织供应商主键
     * @return 结果
     */
    @Override
    public int deleteSysSupplierBySupplierId(Long supplierId)
    {
        return sysSupplierMapper.deleteSysSupplierBySupplierId(supplierId);
    }



    /**
     * 检查用户是否为供应商
     * @param sysUser
     * @return
     */
    @Override
    public boolean checkUserIsSupplier(SysUser sysUser) {
        Long deptId = sysUser.getDeptId();
        SysSupplier sysSupplier = sysSupplierMapper.selectSysSupplierBySupplierId(deptId);
        if(ObjectUtil.isNull(sysSupplier)){
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
